package com.jincp.common.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * =========================================================
 * 类名： DateUtils
 * 类描述： 日期工具类(未特别说明的均为系统默认时区下的时间)
 * 创建日期 16/10/12
 * =========================================================
 */
public class DateUtil {
    /**
     * 1s==1000ms
     */
    private final static int TIME_MILLISECONDS = 1000;
    /**
     * 时间中的分、秒最大值均为60
     */
    private final static int TIME_NUMBERS = 60;
    /**
     * 时间中的小时最大值
     */
    private final static int TIME_HOURS = 24;
    /**
     * 格式化日期的标准字符串
     */
    public final static String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String FORMAT_SPECIAL = "yyyyMMddHHmmss";
    public final static String FORMAT_YMD = "yyyy-MM-dd";
    public final static String FORMAT_HMS = "HH:mm:ss";
    public final static String FORMAT_MS = "mm:ss";

    /**
     * 获取时区信息
     */
    public static TimeZone getTimeZone() {
        return TimeZone.getDefault();
    }

    /**
     * 获得当前日期的字符串
     *
     * @return 日期字符串的Date对象表达形式
     */
    public static String getCurrentDate() {
        return getCurrentDate(FORMAT);
    }

    /**
     * 获得当前日期的字符串
     *
     * @return 日期字符串的Date对象表达形式
     */
    public static String getCurrentDate(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    /**
     * 将日期字符串转换为Date对象
     *
     * @param date 日期字符串，必须为"yyyy-MM-dd HH:mm:ss"
     * @return 日期字符串的Date对象表达形式
     */
    public static Date parseDate(String date) {
        return parseDate(date, FORMAT);
    }

    /**
     * 将日期字符串转换为Date对象
     *
     * @param date   日期字符串，必须为"yyyy-MM-dd HH:mm:ss"
     * @param format 格式化字符串
     * @return 日期字符串的Date对象表达形式
     */
    public static Date parseDate(String date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定的日期的字符串
     *
     * @param format  格式化字符串
     * @param appoint 正整数为当前时间往后几天 负整数为往前几天
     * @return 日期字符串的Date对象表达形式
     */
    public static String getAppointDateString(String format, int appoint) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 将Date对象转换为指定格式的字符串
     *
     * @param date Date对象
     * @return Date对象的字符串表达形式"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatDate(Date date) {
        return formatDate(date, FORMAT);
    }

    /**
     * 将Date对象转换为指定格式的字符串
     *
     * @return Date对象的字符串表达形式"yyyyMMddHHmmss"
     */
    public static String formatDateSepcila(Date date) {
        return formatDate(date, FORMAT_SPECIAL);
    }

    /**
     * 将Date对象转换为指定格式的字符串
     *
     * @param date Date对象
     * @return Date对象的字符串表达形式
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 将日期字符串转换为需要的字符串
     *
     * @param dateString   日期字符串
     * @param importFormat 格式化字符串
     * @param exportFormat 格式化字符串
     * @return 日期字符串
     */
    public static String parseString(String dateString, String importFormat, String exportFormat) {
        try {
            Date date = parseDate(dateString, importFormat);
            return formatDate(date, exportFormat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 格式化日期
     *
     * @param milliseconds 时间毫秒
     * @return 日期字符串"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatMilliseconds(long milliseconds) {
        return formatMilliseconds(milliseconds, FORMAT);
    }

    /**
     * 格式化日期
     *
     * @param milliseconds 时间毫秒
     * @param format   格式化字符串
     * @return 日期字符串
     */
    public static String formatMilliseconds(long milliseconds, String format) {
        Date date = new Date(milliseconds);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        return dateFormat.format(date);
    }

    /**
     * 获取时间戳的Date表示形式
     *
     * @param milliseconds 时间毫秒
     * @return Date对象
     */
    public static Date getDate(long milliseconds) {
        return new Date(milliseconds);
    }

    public static boolean isInScope(String currentDateString, String startDateString, String endDateString, String format) {
        if (TextUtils.isEmpty(currentDateString) || TextUtils.isEmpty(startDateString) || TextUtils.isEmpty(endDateString))
            return true;
        Date currentDate = parseDate(currentDateString, format);
        Date startDate = parseDate(startDateString, format);
        Date endDate = parseDate(endDateString, format);
        return isInScope(currentDate, startDate, endDate);
    }

    public static boolean isInScope(Date currentDate, Date startDate, Date endDate) {
        if (currentDate == null || startDate == null || endDate == null) return true;
        long currentMillis = currentDate.getTime();
        long startMillis = startDate.getTime();
        long endMillis = endDate.getTime();
        if (currentMillis >= startMillis && currentMillis <= endMillis) {
            return true;
        }
        return false;
    }

    public static boolean isSameDay(String dateString1, String dateString2, String format) {
        if (TextUtils.isEmpty(dateString1) || TextUtils.isEmpty(dateString2)) return false;
        Date date1 = parseDate(dateString1, format);
        Date date2 = parseDate(dateString2, format);
        return isSameDay(date1, date2);
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) return false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int year1 = calendar.get(Calendar.YEAR);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(date2);
        int year2 = calendar.get(Calendar.YEAR);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);
        if ((year1 == year2) && (day1 == day2)) {
            return true;
        }
        return false;
    }

    public static String getPrettyDate(String dateString, String formatString) {
        if (TextUtils.isEmpty(dateString)) return "";
        Date start = parseDate(dateString, formatString);
        Calendar calendar = Calendar.getInstance();
        Calendar startCalendar = (Calendar) calendar.clone();
        startCalendar.setTime(start);
        if (calendar.get(Calendar.YEAR) == startCalendar.get(Calendar.YEAR) && calendar.get(Calendar.MONTH) == startCalendar.get(Calendar.MONTH)) {
            if (calendar.get(Calendar.DAY_OF_MONTH) == startCalendar.get(Calendar.DAY_OF_MONTH)) {
                return "今天";
            }
            int today = calendar.get(Calendar.DAY_OF_MONTH);
            int temp = startCalendar.get(Calendar.DAY_OF_MONTH) + 1;
            if (today == temp) {
                return "昨天";
            }
        }
        return dateString;

    }

    /**
     * 将总秒数转换为时分秒表达形式
     *
     * @param seconds 任意秒数
     * @return %s小时%s分%s秒
     */
    public static String formatTime(long seconds) {
        long ss = seconds % TIME_NUMBERS;
        long m = seconds / TIME_NUMBERS;
        long mm = m % TIME_NUMBERS;
        long hour = m / TIME_NUMBERS;
        long hh = hour % TIME_HOURS;
        long dd = hour / TIME_HOURS;
        return (dd == 0 ? "" : (dd < 10 ? "0" + dd : dd) + "天 ")
                + (hh == 0 ? "" : (hh < 10 ? "0" + hh : hh) + ":")
                + (mm == 0 ? "00:" : (mm < 10 ? "0" + mm : mm) + ":")
                + (ss == 0 ? "00" : (ss < 10 ? "0" + ss : ss) + "");
    }

    /**
     * 将总秒数转换为时分秒表达形式
     * 一天使用八小时
     *
     * @param seconds 任意秒数
     * @return %s小时%s分%s秒
     */
    public static String formatTime8D(long seconds) {
        long ss = seconds % TIME_NUMBERS;
        long m = seconds / TIME_NUMBERS;
        long mm = m % TIME_NUMBERS;
        long hour = m / TIME_NUMBERS;
        long hh = hour % 8;
        long dd = hour / 8;
        return (dd == 0 ? "" : (dd < 10 ? "0" + dd : dd) + "d ")
                + (hh == 0 ? "" : (hh < 10 ? "0" + hh : hh) + "h")
                + (mm == 0 ? "00:" : (mm < 10 ? "0" + mm : mm) + "m")
                + (ss == 0 ? "00" : (ss < 10 ? "0" + ss : ss) + "s");
    }

    /**
     * 获取当前时间距离指定日期时差的大致表达形式
     *
     * @param date 日期
     * @return 时差的大致表达形式
     */
    public static String getDiffTime(long date) {
        String strTime = formatDate(new Date(date), FORMAT);
        long time = Math.abs(new Date().getTime() - date);
        // 一分钟以内
        if (time < TIME_NUMBERS * TIME_MILLISECONDS) {
            strTime = "刚刚";
        } else {
            int min = (int) (time / TIME_MILLISECONDS / TIME_NUMBERS);
            if (min < TIME_NUMBERS) {
                if (min < 15) {
                    strTime = "15分钟前";
                } else if (min < 30) {
                    strTime = "半小时前";
                } else {
                    strTime = "1小时前";
                }
            } else {
                int hh = min / TIME_NUMBERS;
                if (hh < TIME_HOURS) {
                    strTime = hh + "小时前";
                } else {
                    int days = hh / TIME_HOURS;
                    if (days <= 6) {
                        strTime = days + "天前";
                    } else {
                        int weeks = days / 7;
                        if (weeks < 3) {
                            strTime = weeks + "周前";
                        }
                    }
                }
            }
        }
        return strTime;
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {   //同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {   //闰年
                    timeDistance += 366;
                } else  {  //不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else {  //不同年
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    public static String get10Date(String startDate) {
        if (TextUtils.isEmpty(startDate)) return "";
        if (startDate.length() > 10) {
            return startDate.substring(0, 10);
        }
        return startDate;
    }
}
